Ensure stable routing for x-modulus-hash exchange#15849
Merged
Conversation
6900588 to
f4ffd85
Compare
## What? This commit makes routing stable for the `x-modulus-hash` exchange: If the same destination queues stay bound to the exchange (i.e. do not bind or unbind queues after the "initial setup"), messages with the same domain entity (routing key) will always end up in the same destination queue, even across node restarts. ## How? With Mnesia this was guaranteed due to order_set tables. Khepri introduced a regression since it uses a bag ETS projection table. This commit simply sorts the destinations before picking the Nth destination. ## Why? Uses cases where message order matters are common. This commit allows to for example bind N quorum queues to an `x-modulus-hash` exchange instance (the binding key doesn't matter) and use the Single Active Consumer (SAC) feature on each quorum queue. This will provide * message ordering thanks to stable routing and SAC * concurrent consumption: N app instances can process messages in parallel * fault tolerance since the broker will deliver messages to another consumer when the active consumer crashes Using the consistent hash exchange is an alternative, but unnecessarily complex for this use case. Yet another alternative for this use case is using the murmur3 exchange type (#8319).
Move the x-modulus-hash exchange type from rabbitmq_sharding to rabbit since this exchange type is useful even without the sharding plugin.
This commit adds a test case to explicitly verify that the `x-modulus-hash` exchange supports weighted routing. Because the exchange uses `lists:sort/1` instead of `lists:usort/1` when fetching the bound destinations, a user can bind the exact same queue multiple times to the exchange (using different dummy binding keys) to increase its weight. If a queue is bound N times, it will appear N times in the destination list, giving it a proportionally higher chance of receiving messages.
f4ffd85 to
d7b3d19
Compare
kjnilsson
approved these changes
Mar 27, 2026
ansd
added a commit
to rabbitmq/rabbitmq-website
that referenced
this pull request
Mar 27, 2026
This commit adds documentation for the built-in `x-modulus-hash` exchange. It adds a dedicated page explaining its core features: stable routing, weighted routing, and how it compares to the consistent hash exchange. It also adds a bullet point to the message ordering section in `queues.md` to highlight the common use case of using `x-modulus-hash` with Single Active Consumer (SAC) to process messages concurrently while preserving order for each domain entity. Docs for rabbitmq/rabbitmq-server#15849
ansd
added a commit
to rabbitmq/rabbitmq-website
that referenced
this pull request
Mar 27, 2026
This commit adds documentation for the built-in `x-modulus-hash` exchange. It adds a dedicated page explaining its core features: stable routing, weighted routing, and how it compares to the consistent hash exchange. It also adds a bullet point to the message ordering section in `queues.md` to highlight the common use case of using `x-modulus-hash` with Single Active Consumer (SAC) to process messages concurrently while preserving order for each domain entity. Docs for rabbitmq/rabbitmq-server#15849
ansd
added a commit
that referenced
this pull request
Mar 27, 2026
Ensure stable routing for `x-modulus-hash` exchange (backport #15849)
michaelklishin
added a commit
that referenced
this pull request
Mar 27, 2026
michaelklishin
added a commit
that referenced
this pull request
Mar 27, 2026
michaelklishin
added a commit
that referenced
this pull request
Mar 27, 2026
…-2026 Add #15849 to `4.3.0` release notes
acogoluegnes
added a commit
to rabbitmq/rabbitmq-java-client
that referenced
this pull request
Mar 30, 2026
RabbitMQ 4.3+. References rabbitmq/rabbitmq-server#15849 References #1918
acogoluegnes
added a commit
to rabbitmq/rabbitmq-java-client
that referenced
this pull request
Mar 31, 2026
RabbitMQ 4.3+. References rabbitmq/rabbitmq-server#15849 References #1918 (cherry picked from commit 9c41e2c)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
This PR makes routing stable for the
x-modulus-hashexchange: If the same destination queues stay bound to the exchange (i.e. do not bind or unbind queues after the "initial setup"), messages with the same domain entity (routing key) will always end up in the same destination queue, even across node restarts.Move the x-modulus-hash exchange type from
rabbitmq_shardingtorabbitsince this exchange type is useful even without the sharding plugin.How?
With Mnesia this was guaranteed due to order_set tables. Khepri introduced a regression since it uses a bag ETS projection table.
This PR simply sorts the destinations before picking the Nth destination.
Why?
Uses cases where message order matters are common. This PR allows to for example bind N quorum queues to an
x-modulus-hashexchange instance (the binding keys do not matter) and use the Single Active Consumer (SAC) feature on each quorum queue. This will provideUsing the consistent hash exchange is an alternative, but unnecessarily complex for this use case.
Yet another alternative for this use case is using the murmur3 exchange type (#8319).
Docs:
rabbitmq/rabbitmq-website#2494